import axios from "axios";

const request = axios.create({
  baseURL: "http://localhost:3000",
  timeout: 5000,
});

request.interceptors.request.use(
  (config) => {
    // 如果config里配置了不需要token，就不添加token
    if (!config.notNeedToken) {
      // 添加token
      config.headers.Authorization = sessionStorage.getItem("bike_token");
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  (response) => {
    if (response.status === 200 || response.status === 304) {
      return response.data;
    }
    // TODO：错误处理
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default request;
